﻿@using Orchard.Widgets.Models;
@{
    Style.Require("WidgetsAdmin");
    Script.Require("jQuery");
    IEnumerable<LayerPart> layers = Model.Layers;
}
@functions {
    static string EncodeLineBreaks(string text) {
		if (!String.IsNullOrEmpty(text)){
			return text.Replace(Environment.NewLine, "\\\\n");
		}
	
		return text;
    }
}
<script type="text/javascript">
//<![CDATA[
    document.write("<div id=\"widgets-layer-visibility\"><h3>@T("All Layers:")</h3></div>");
//]]>
</script>
@using(Script.Foot()) {
<script type="text/javascript">
//<![CDATA[
    (function ($) {
        var currentLayerId = @Model.CurrentLayer.Id;
        var layers = [
        @foreach (var layer in layers) {
            <text>{"name":"@layer.Name","description":"@EncodeLineBreaks(layer.Description)","id":@layer.Id}@(layer != layers.Last() ? "," : "")</text>
        }
        ];
        var visWrapper = $("#widgets-layer-visibility");
        var visContainer = $("<ul></ul>");
    
        var widgetPlacement = $("#widgets-zones");
        for (var i = 0; i < layers.length; i++) {
            var layersWidgets = widgetPlacement.find(".widgets-layer-" + layers[i].id);
            var layer = "";

            if (layersWidgets.length > 0) {
                layer = $("<li title=\"" + layers[i].description + "\" class=\"widgets-" + ((layers[i].id == currentLayerId && "this") || "other") + "-layer\">" + layers[i].name + "</li>");
                layer.data("widgets", layersWidgets);
                layersWidgets.each(function(){
                    $(this).data("widgets-vis-control", layer);
                });
            }
            else {
                layer = $("<li title=\"" + layers[i].description + "\" class=\"widgets-empty-layer" + ((layers[i].id == currentLayerId && " widgets-this-layer") || "") + "\">" + layers[i].name + " <span>[empty]</span></li>");
            }

            visContainer.append(layer);
        }

        $("#widgets-layer-visibility .widgets-other-layer").live("click", function() {
            var _this = $(this);
            _this.toggleClass("off");
            if (_this.hasClass("off")) {
                _this.data("widgets").hide();
            }
            else {
                _this.data("widgets").show();
            }
        });
        $("#widgets-layer-visibility .widgets-other-layer").live("hover", function() {
            $(this).data("widgets").toggleClass("on");
        });
        $("#widgets-zones .widgets-other-layer").live("hover", function() {
            var layer = $(this).data("widgets-vis-control");
            layer.toggleClass("on");
            layer.data("widgets").toggleClass("on");
        });

        visWrapper.append(visContainer);
    })(jQuery);
//]]>
</script>
}
